package com.qqbot.impl.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qqbot.core.message.MessageChain;
import com.qqbot.impl.service.SinaWeiBoService;
import com.qqbot.impl.utils.HttpClientUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 新浪微博相关服务
 *
 * @author changwenpeng
 * @date 2022年5月10日
 */
@Service
public class SinaWeiBoServiceImpl implements SinaWeiBoService {
    @Override
    public MessageChain getWaterImage() throws Exception {
        String url = "https://weibo.com/ajax/statuses/mymblog?uid=1958436973&page=1&feature=0";
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36");
        headers.put("Cookie", "SUBP=0033WrSXqPxfM72-Ws9jqgMF55529P9D9WF_yqAv1Y1gneb7yxnUf1s.; SINAGLOBAL=4800376860047.213.1610948045626; UOR=,,www.baidu.com; SUB=_2AkMXIZAVf8NxqwJRmP0Ty2PgbIl2zQnEieKhfWHOJRMxHRl-yj9kqnJatRB6PKG--mbP3O_wAbPnBwxNKRhTKGlZpwaI; ULV=1633594873082:7:2:2:5387699613687.524.1633594873003:1633576476216; XSRF-TOKEN=meXtUKfWZJ-Fn-B8l8OHNAL8; WBPSESS=a_YZA6I5qCR3U8i3Rfvlps0Q-XJFJycZ44BivcouGhkq-IUU6w0AG9sZGaeb5IbNT8f-4bwy3eMdfySNPVREk3GEjHDGvb3sW69FhWO1UnEr98yaW-SM6H_gdqTjXZ1Wb0NpPPhT_Ga3IRmvuUJljVL6wTOuizGThVYpMmadNAE=");
        headers.put("host", "weibo.com");
        String content = HttpClientUtils.get(url, headers, Integer.valueOf(10000), Integer.valueOf(10000));
        if (StringUtils.isEmpty(content)) {
            return new MessageChain();
        }
        JSONObject jsonObject = JSONObject.parseObject(content);
        JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("list");
        JSONObject jsonObject1 = (JSONObject) jsonArray.get(0);
        JSONArray jsonArray1 = jsonObject1.getJSONArray("pic_ids");
        return new MessageChain().text("小提示：该喝水了！\n").image("http://wx1.sinaimg.cn/orj1080/" + jsonArray1.get(0));
    }
}
